﻿// Copyright (c) Microsoft Corporation.  All rights reserved.
// This source code is made available under the terms of the Microsoft Public License (MS-PL)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

namespace Test
{
	/// <summary>
	/// A basic abstract LINQ query provider
	/// </summary>
	public abstract class QueryProvider : IQueryProvider, IQueryText
	{
		protected QueryProvider() {
		}

		IQueryable<S> IQueryProvider.CreateQuery<S>(Expression expression) {
			return new Query<S>(provider:this, expression: expression, id: 0);
		}

		IQueryable IQueryProvider.CreateQuery(Expression expression) {
			Type elementType = TypeHelper.GetElementType(expression.Type);
			try {
				return (IQueryable)Activator.CreateInstance(typeof(Query<>).MakeGenericType(elementType), new object[] { this, expression });
			}
			catch (TargetInvocationException tie) {
				throw tie.InnerException;
			}
		}

		S IQueryProvider.Execute<S>(Expression expression) {
			return (S)this.Execute(expression);
		}

		object IQueryProvider.Execute(Expression expression) {
			return this.Execute(expression);
		}

		public abstract string GetQueryText(Expression expression);
		public abstract object Execute(Expression expression);
	}
}
